<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\TeamWorkUser;
use App\Models\AdminUser;
use App\Models\Team;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;

class TeamController extends AdminController
{
    protected $title = '队伍';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Team(), function (Grid $grid) {
            $grid->disableViewButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->like('name','名称')->width(5);
            });

            $grid->number('序号');
//            $grid->column('name');

            if (Admin::user()->can('领导层')) {
                $grid->column('name')->modal(function (Grid\Displayers\Modal $modal) {
                    $modal->xl();
                    $modal->title('所属劳务人员');
                    $modal->icon('feather icon-edit');
                    return TeamWorkUser::make()->payload(['team' => $this->name]);
                });
            }else{
                $grid->column('name');
            }

            $grid->column('count','当前在职人数')->display(function ($name) {
                return DB::table('work_user')->where('team',$this->name)->where('deleted_at',null)->where('status',1)->count();
            });

            $grid->column('status','状态')->display(function ($status) {
                if($status == 1){
                    return "<span style='color:blue'>正常</span>";
                }else{
                    return "<span style='color:red'>关闭</span>";
                }
            });
//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Team(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Team(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->radio('status')->options(['1' => '正常', '2'=> '关闭'])->default('1');

            $form->display('created_at');
            $form->display('updated_at');


            $form->saving(function (Form $form) {
                $cf = DB::table('team')->where('name',$form->name)->where('deleted_at',null)->first();
                if(!empty($cf)){
                    return $form->response()->error('该队伍已经存在');
                }
            });


        });
    }
}
